const pool = require('./index.js')

class user_db {
	//查询所有userinfo表信息
	static async getAllUserInfo(){
		try{
			const [res, fields] = await pool.execute('SELECT * FROM userinfo')
			// console.log('res',res)
			return res
		}catch(err){
			console.log('getAllUserInfo_err:',err)
			return err
		}
	}
	
	//通过key获取用户信息,key默认值是id
	static async getUserInfo(keyValue, key = 'id'){
		try{
			const validKeys = ['id', 'userName']  //允许查询的key字段
			if(!keyValue){
				throw new Error('查询值错误')
			}
			if(!validKeys.includes(key)){
				throw new Error(`${key}字段无法进行查询`)
			}
			const [res, fields] = await pool.execute(`SELECT * FROM userinfo WHERE ${key} = ?`, [keyValue])
			// console.log('res',res)
			return res
		}catch(err){
			console.log('getUserInfo_err:',err.message)
			return err
		}
	}
	
}

module.exports = user_db